package ml;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.List;

public class ProcessExecutionUtil {

	public static String executeProcess(final List<String> params,
			final String workingDirectory) {
		final StringBuilder sbOutput = new StringBuilder();

		try {
			final ProcessBuilder processBuilder = new ProcessBuilder(params);
			processBuilder.directory(new File(workingDirectory));
			processBuilder.redirectErrorStream(true);
			final Process process = processBuilder.start();

			String readLine = null;
			final BufferedReader output = new BufferedReader(
					new InputStreamReader(process.getInputStream()));
			while ((readLine = output.readLine()) != null) {
				sbOutput.append(readLine);
				System.out.println(readLine);
			}

			process.waitFor();
		} catch (final Exception e) {
			System.out.println(e.getMessage());
		}

		return sbOutput.toString();
	}
}
